package ding.utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;

public class FileImageUtil {
    public static String fileUploadUrl(HttpServletRequest req, String path, Part part){
        //指定上传路径
        String fileUploadUrl = req.getSession().getServletContext().getRealPath("upload/");
        //logger.debug("指定上传路径" + fileUploadUrl);
        System.out.println("指定上传路径" + fileUploadUrl);
        //file判断文件夹是否存在
        File file = new File(fileUploadUrl);
        //判断上传路径是否存在，不存在新建目录
        if (!file.exists()){
            file.mkdir();
        }
        //获取上传文件名
        String fileName = FileUtil.jxFileName(part);
        //获取上传服务器
        try {
            part.write(fileUploadUrl+File.separator+fileName);
            return path+"/"+fileName;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
